package com.gugee.ins.common.dal.service.blogger.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.blogger.InsBloggerAddExtMapper;
import com.gugee.ins.common.dal.service.blogger.InsBloggerAddDao;
import com.gugee.ins.common.model.ai.InsBloggerLanguage;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerAdd;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsBloggerAddDaoImpl extends ServiceImpl<InsBloggerAddExtMapper, InsBloggerAdd> implements InsBloggerAddDao {


    /**
     * 更新博主时间
     * @param uid
     */
    @Override
    public void updateCollectTime(long uid) {
        InsBloggerAdd insBloggerAdd = new InsBloggerAdd();
        insBloggerAdd.setUid(uid);
        insBloggerAdd.setLastCollectTime(System.currentTimeMillis() / 1000);
        updateByUidSelective(insBloggerAdd);
    }

    /**
     * 更新帖子采集时间
     * @param uid
     */
    @Override
    public void updateMediaCollectTime(long uid) {
        InsBloggerAdd insBloggerAdd = new InsBloggerAdd();
        insBloggerAdd.setUid(uid);
        insBloggerAdd.setLastMediaCollectTime(System.currentTimeMillis() / 1000);
        updateByUidSelective(insBloggerAdd);
    }

    /**
     * 更新评论采集
     * @param uid
     * @param commentCollectCount
     */
    @Override
    public void updateCommentCollectTime(long uid,int commentCollectCount) {
        InsBloggerAdd insBloggerAdd = new InsBloggerAdd();
        insBloggerAdd.setUid(uid);
        insBloggerAdd.setLastCommentCollectTime(System.currentTimeMillis() / 1000);
        updateByUidSelective(insBloggerAdd);
    }


    /**
     * 更新关注人列表采集时间
     * @param uid
     */
    @Override
    public void updateFollowingCollectTime(long uid) {
        InsBloggerAdd insBloggerAdd = new InsBloggerAdd();
        insBloggerAdd.setUid(uid);
        insBloggerAdd.setLastFollowingCollectTime(System.currentTimeMillis() / 1000);
        updateByUidSelective(insBloggerAdd);
    }

    /**
     * 更新粉丝列表采集时间
     * @param uid
     */
    @Override
    public void updateFollowerCollectTime(long uid) {
        InsBloggerAdd insBloggerAdd = new InsBloggerAdd();
        insBloggerAdd.setUid(uid);
        insBloggerAdd.setLastFollowerCollectTime(System.currentTimeMillis() / 1000);
        updateByUidSelective(insBloggerAdd);
    }

    /**
     * 根据uid查找数据
     * @param uid
     * @return
     */
    @Override
    public InsBloggerAdd findByUid(long uid){
        return baseMapper.findByUid(uid);
    }

    /**
     * 根据uid条件更新
     * @param insBloggerAdd 数据
     * @return
     */
    @Override
    public void updateByUidSelective(InsBloggerAdd insBloggerAdd){
        UpdateWrapper<InsBloggerAdd> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid",insBloggerAdd.getUid());
        baseMapper.update(insBloggerAdd,updateWrapper);
    }

    /**
     * 批量收录
     * @param insBloggerMessageList
     * @return
     */
    @Override
    public void batchInsert(List<InsBloggerMessage> insBloggerMessageList){
        long nowTime = System.currentTimeMillis() / 1000;
        try{
            baseMapper.batchInsert(insBloggerMessageList,nowTime);
        }catch (Exception e){
            e.printStackTrace();
            log.debug("batch insert ins bloggerAdd failed ,try insert one by one, msg : {} ",e.getMessage());
            insBloggerMessageList.forEach(insBloggerMessage -> {
                try{
                    baseMapper.insertOne(insBloggerMessage,nowTime);
                }catch (Exception ex){
                    e.printStackTrace();
                    log.info("ins bloggerAdd collect failed ,uid : {} , msg : {} "
                            ,insBloggerMessage.getUid(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 批量更新分析语言的时间
     * @param insBloggerLanguages
     */
    @Override
    public void batchUpdateAnalysisLanguageTime(List<InsBloggerLanguage> insBloggerLanguages){
        long nowTime = System.currentTimeMillis() / 1000;
        try{
            baseMapper.batchUpdateAnalysisLanguageTime(insBloggerLanguages,nowTime);
        }catch (Exception e){
            e.printStackTrace();
            log.debug("batch Update Analysis Language Time failed ,try Update one by one, msg : {} ",e.getMessage());
            insBloggerLanguages.forEach(insBloggerLanguage -> {
                try{
                    baseMapper.updateAnalysisLanguageTime(insBloggerLanguage.getUid(),nowTime);
                }catch (Exception ex){
                    e.printStackTrace();
                    log.info("Update Analysis Language Time failed ,uid : {} , msg : {} "
                            ,insBloggerLanguage.getUid(),ex.getMessage());
                }
            });
        }
    }
}
